package handler

import (
	"5thweektest1/model"
	"github.com/gin-gonic/gin"
)

func Add(c *gin.Context) {
	title := c.PostForm("title")
	content := c.PostForm("content")
	author := c.PostForm("author")
	if title == "" || content == "" || author == "" {
		c.JSON(200, gin.H{
			"code": 500,
			"msg":  "字段不可为空",
		})
		return
	}
	data := model.Movie{
		Title:   title,
		Content: content,
		Author:  author,
	}
	model.DB.Create(&data)
	c.JSON(200, gin.H{
		"code": 200,
		"msg":  "添加成功",
	})
	return
}
func List(c *gin.Context) {
	var data []model.Movie
	if err := model.DB.Find(&data).Error; err != nil {
		c.JSON(200, gin.H{
			"code": 500,
			"msg":  "列表展示失败",
		})
		return
	}
	c.JSON(200, gin.H{
		"code": 200,
		"data": data,
		"msg":  "success",
	})
	return
}
